﻿using Search4.Domain.Repository;
using System;
using System.Web.UI.WebControls;

namespace Search4.Web.View.Cadastro
{
    public class Login : System.Web.UI.Page
    {
        #region CONTROLES
        public LinkButton lbtCadastro { get; set; }
        public Button btnLogin { get; set; }
        public TextBox txbLogin { get; set; }
        public TextBox txbSenha { get; set; }
        public Label lblErro { get; set; }
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            VerificaSeUsuarioEstaLogado();
            ConfigurarPagina();
        }

        private void ConfigurarPagina() 
        {
            lbtCadastro.Click += lbtCadastro_Click;
            btnLogin.Click += btnLogin_Click;
        }

        private void VerificaSeUsuarioEstaLogado()
        {
            var lbtPainel = (LinkButton)Master.FindControl("lbtPainel");
            var lblSeparador = (Label)Master.FindControl("lblSeparador");
            var lbtCadastro = (LinkButton)Master.FindControl("lbtCadastro");
            var lblWelcome = (Label)Master.FindControl("lblWelcome");
            var lbtLogin = (LinkButton)Master.FindControl("lbtLogin");
            var lbtLogout = (LinkButton)Master.FindControl("lbtLogout");
            var usuarioLogado = Session["USUARIO"] != null ? true : false;
            if (usuarioLogado)
            {
                lbtLogin.Visible = false;
                lbtCadastro.Visible = false;
                lbtLogout.Visible = true;
                lblWelcome.Visible = true;
                lbtPainel.Visible = true;
                lblSeparador.Visible = true;
            }
            else
            {
                lbtLogin.Visible = true;
                lbtCadastro.Visible = true;
                lbtLogout.Visible = false;
                lblWelcome.Visible = false;
                lbtPainel.Visible = false;
                lblSeparador.Visible = false;
            }
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            var usuarioRepository = new UsuarioRepository();

            if (!String.IsNullOrEmpty(txbLogin.Text) && !String.IsNullOrEmpty(txbSenha.Text))
            {
                var usuario = usuarioRepository.ObterPorEmailESenha(txbLogin.Text, Convert.ToInt32(txbSenha.Text));

                if (usuario != null)
                {
                    lblErro.Visible = false;
                    Session["USUARIO"] = usuario.IdUsuario;
                    Response.Redirect("../Default.aspx");
                }
                else
                {
                    lblErro.Visible = true;
                }
            }
            else
            {
                lblErro.Visible = true;
            }
        }

        void lbtCadastro_Click(object sender, EventArgs e)
        {
            Response.Redirect("Usuario.aspx");
        }

    }
}

